﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Jzy.BLL.CRM;
using Jzy.Model.CRM;
using Jzy.Core.Common.Web;

using Frame.Lib.Common;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Dict;
using Frame.Lib.Enums;

namespace Jzy.Web.CRM.Customer
{
    public partial class Edit : PageEdit
    {
        private string CustomerID
        {
            get
            {
                return Request["CustomerID"];
            }
        }

        private CustomerBO _customer;
        private CustomerBO Customer
        {
            get
            {
                if (_customer == null)
                {
                    _customer = new CustomerBO();
                }
                return _customer;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DictBO dict = new DictBO();
                dict.BindListControl(Level, (int)DictItem.客户级别);

                if (!string.IsNullOrEmpty(this.CustomerID))
                {
                    DispInfo();
                }
                AddAttr();
            }

            AjaxPro.Utility.RegisterTypeForAjax(typeof(Edit));

        }

        private void AddAttr()
        {
            //onclick="setday(this,true);"
            this.NextContactTime.Attributes.Add("onclick", "setday(this,false)");
        }

        private void DispInfo()
        {
            CustomerInfo info = this.Customer.GetInfo(CustomerID);

            EntityUtil.SetControl(pnlMain, info);

            NextContactTime.Text = info.NextContactTime == null ? "" : ((DateTime)info.NextContactTime).ToString("yyyy-MM-dd");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            CustomerInfo info = this.Customer.GetInfo(CustomerID);
            EntityUtil.SetEntity(pnlMain, info);

            if (string.IsNullOrEmpty(this.CustomerID))
            {
                info.InputTime = DateTime.Now;
                info.Orders = this.Customer.GetMaxOrder();
                info.CreatorID = AppSession.UserID;
                info.CreatorName = AppSession.UserName;              
            }

            bool flag = this.Customer.Exist(Name.Text,this.CustomerID) || this.Customer.ExistCode(Code.Text, this.CustomerID);
            if (flag)
            {
                JSUtil.Alert("该客户名称或编号已存以");
                return;
            }

            this.Customer.Save(info);

            if (string.IsNullOrEmpty(this.CustomerID) && this.ContactName.Text.Trim() != "")
            {
                CustomerContactBO contract = new CustomerContactBO();
                CustomerContactInfo conInfo = new CustomerContactInfo();
                conInfo.CustomerID = info.ID;
                conInfo.OfficePhone = this.Telephone.Text;
                conInfo.Name = this.Name.Text;

                contract.Save(conInfo);
            }

            if (string.IsNullOrEmpty(CustomerID))
            {
                string js = "alert('保存成功');window.location.href='../contact/list.aspx?CustomerID=" + info.ID + "';";
                JSUtil.ResponseJS(js);
            }
            else
            {
                string js = "alert('修改成功');window.location.href='list.aspx';";
                JSUtil.ResponseJS(js);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("List.aspx");
        }


        [AjaxPro.AjaxMethod]
        public bool CheckTime(string strTime)
        {
            try
            {
                DateTime tmp = DateTime.Parse(strTime);
                return true;
            }
            catch (FormatException)
            {
                return false;
            }
        }
    }
}
